package com.michale.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.michale.common.domain.system.DictDataEntity;
import com.michale.common.utils.String.StringUtils;
import com.michale.common.utils.dict.DictUtils;
import com.michale.common.utils.security.SecurityUtils;
import com.michale.system.mapper.system.DictDataMapper;
import com.michale.system.service.DictDataService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Michale
 * @apiNote 字典数据管理 业务层
 */
@Service("dictDataService")
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictDataEntity> implements DictDataService {

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictDataEntity 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<DictDataEntity> selectDictDataList(DictDataEntity dictDataEntity) {
        return baseMapper.selectDictDataList(dictDataEntity);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return baseMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public DictDataEntity selectDictDataById(Long dictCode) {
        return baseMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            DictDataEntity dictDataEntity = selectDictDataById(dictCode);
            baseMapper.deleteDictDataById(dictCode);
            List<DictDataEntity> dictDataList = baseMapper.selectDictDataByType(dictDataEntity.getDictType());
            DictUtils.setDictCache(dictDataEntity.getDictType(), dictDataList);
        }
    }

    /**
     * 新增字典数据信息
     *
     * @param dictDataEntity 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(DictDataEntity dictDataEntity) {
        dictDataEntity.setCreateBy(SecurityUtils.getUsername());
        int row = baseMapper.insertDictData(dictDataEntity);
        if (row > 0) {
            List<DictDataEntity> dictDataList = baseMapper.selectDictDataByType(dictDataEntity.getDictType());
            DictUtils.setDictCache(dictDataEntity.getDictType(), dictDataList);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictDataEntity 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(DictDataEntity dictDataEntity) {
        dictDataEntity.setUpdateBy(SecurityUtils.getUsername());
        int row = this.baseMapper.updateDictData(dictDataEntity);
        if (row > 0) {
            List<DictDataEntity> dictDataList = baseMapper.selectDictDataByType(dictDataEntity.getDictType());
            DictUtils.setDictCache(dictDataEntity.getDictType(), dictDataList);
        }
        return row;
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param  dictTypeEntity 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<DictDataEntity> selectDictDataByType(String dictTypeEntity) {
        List<DictDataEntity> dictDataList = DictUtils.getDictCache(dictTypeEntity);
        if (StringUtils.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        dictDataList = baseMapper.selectDictDataByType(dictTypeEntity);
        if (StringUtils.isNotEmpty(dictDataList)) {
            DictUtils.setDictCache(dictTypeEntity, dictDataList);
            return dictDataList;
        }
        return null;
    }
}